package game.module.ddz.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.ddz.bean.DdzRound;
import game.module.ddz.logic.DdzDataManager;
import game.module.ddz.logic.DdzLogic;
import game.session.SessionManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageDdz.C2SDdzForceDismisroom;
import lua.LuaMessageDdz.PushDdzForceDismissRoom;
import lua.LuaMessageDdz.S2CDdzForceDismisroom;
import lua.LuaMessageLogin.S2ErrorCode;

@MsgCodeAnn(msgcode = C2SDdzForceDismisroom.id, accessLimit = 500)
public class DdzFoceDismissRoom extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(DdzFoceDismissRoom.class);

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		int playerId = playingRole.getId();
		logger.info("ddz force to dismiss room,playerId={}", playerId);
		// 房间是否存在
		// 房间是否存在
		DdzRound ddzround = DdzDataManager.getInstance().getDdzRoom(playerId);
		if (ddzround == null) {
			S2ErrorCode respMsg = new S2ErrorCode(201);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		S2CDdzForceDismisroom msg = new S2CDdzForceDismisroom();
		playingRole.writeAndFlush(msg.build(playingRole.alloc()));
		List<Integer> players = ddzround.getPlayerIds();
		PushDdzForceDismissRoom pushmsg = new PushDdzForceDismissRoom();
		pushmsg.player_index = (byte) players.indexOf(playerId);
		for(Integer aplayer:players) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aplayer);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushmsg.build(pr.alloc()));
				// 推送关于出牌的信息
			}
		}
		DdzLogic.getInstance().roundOver(ddzround);
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {

	}

}
